#include "common.h"

int show_bmp(int x, int y, char *filename)
{
    // 1.打开图片
    int fd = open(filename, O_RDWR);
    if (fd == -1)
    {
        perror(filename);
        return -1; // 返回错误代码
    }

    // 2.读取数据
    // 读取宽、高、色深
    int width = 0, height = 0, depth = 0;
    if (lseek(fd, 0x12, SEEK_SET) == -1 ||
        read(fd, &width, 4) != 4 ||
        lseek(fd, 0x16, SEEK_SET) == -1 ||
        read(fd, &height, 4) != 4 ||
        lseek(fd, 0x1c, SEEK_SET) == -1 ||
        read(fd, &depth, 4) != 4)
    {
        perror("Error reading image header");
        close(fd);
        return -1; // 返回错误代码
    }

    // 读取像素点的数据
    int laizi = 0;     // 表示要填充的字节数
    int line_size = 0; // 实际的字节数

    laizi = 4 - (abs(width) * (depth / 8)) % 4;
    if (laizi == 4)
    {
        laizi = 0;
    }

    // 实际的字节数 = 有效字节数 + 赖子
    line_size = abs(width) * (depth / 8) + laizi;

    // 一口气把所有像素点数据全部读取出来
    unsigned char buf[abs(height) * line_size];
    if (lseek(fd, 0x36, SEEK_SET) == -1 ||
        read(fd, buf, abs(height) * line_size) != abs(height) * line_size)
    {
        perror("Error reading pixel data");
        close(fd);
        return -1; // 返回错误代码
    }

    // 3.显示
    int color;
    unsigned b, g, r, a = 0;
    int i, j;
    int num = 0;

    for (j = 0; j < abs(height); j++)
    {
        for (i = 0; i < abs(width); i++)
        {
            // 合成颜色数据
            b = buf[num++];
            g = buf[num++];
            r = buf[num++];
            if (depth == 32)
            {
                a = buf[num++];
            }
            color = (a << 24) | (r << 16) | (g << 8) | b;

            // 画点
            display_point((width > 0 ? i : abs(width) - 1 - i) + x,
                          (height > 0 ? abs(height) - 1 - j : j) + y,
                          color);
        }

        num = num + laizi; // 跳过无效的数据
    }

    // 4.关闭图片
    close(fd);

    return 0; // 返回成功代码
}
char *name[5] = {"01.bmp", "02.bmp", ""};

int Circle_show_bmp()
{
    int i;
    for (i = 0; i < 5; i++)
    {
        show_bmp(0, 0, name[i]);
        sleep(1);
    }
}

#define MAX_FILENAME_LENGTH 256
char BMPS[100][1000];
int indx = 0;
int is_bmp_file(const char *filename)
{
    size_t len = strlen(filename);
    return len >= 4 && strcmp(filename + len - 4, ".bmp") == 0;
}
void album(int agc, char *argv[])
{
    DIR *dir;
    struct dirent *entry;
    struct stat file_stat;
    if (agc < 2)
    {
        dir = opendir("./photos");
    }
    // 打开目录
    else
    {
        dir = opendir(argv[1]);
        printf("%s", argv[1]);
    }
    if (dir == NULL)
    {
        perror("opendir");
        exit(EXIT_FAILURE);
    }

    // 遍历目录
    while ((entry = readdir(dir)) != NULL)
    {
        if (stat(entry->d_name, &file_stat) == -1)
        {
            perror("stat");
            exit(EXIT_FAILURE);
        }

        // 判断是否为文件且文件名以.bmp结尾
        if (S_ISREG(file_stat.st_mode) && is_bmp_file(entry->d_name))
        {
            // printf("%s\n", entry->d_name);
            strcpy(BMPS[indx++], entry->d_name);
        }
    }
    lcd_init();
    show_bmp(0, 0, BMPS[0]);
    int i = 0;
    while (1)
    {
        int op = get_touch();
        printf("op = %d\n", op);
        if (op == 1 || op == 4)
            i = (i + 1) % indx;
        else if (op == 2 || op == 3)
            i = (i - 1 + indx) % indx;
        show_bmp(0, 0, BMPS[i]);
    }
    closedir(dir);
}